package com.zhihuishu.micro.course.service.impl;

import com.zhihuishu.micro.course.dao.CourseFolderMapper;
import com.zhihuishu.micro.course.dto.CourseFolderDto;
import com.zhihuishu.micro.course.dto.CourseFolderRootAndDirDto;
import com.zhihuishu.micro.course.dto.LessonDto;
import com.zhihuishu.micro.course.model.CourseFolder;
import com.zhihuishu.micro.course.model.Lesson;
import com.zhihuishu.micro.course.service.CCFolderTreenityDBService;
import com.zhihuishu.toolkit.helper.BeanHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * table-CC_FOLDER
 * dto-CourseFolder
 * @author Jiangli
 * @date 2016/12/16 13:15
 */
@Service("ccFolderTreenityServiceDB")
public class CCFolderTreenityDBServiceImpl extends BaseTreenityBatchServiceDBImpl<CourseFolderDto,CourseFolder> implements CCFolderTreenityDBService {
    @Resource
    private CourseFolderMapper courseFolderMapper;

    @Override
    public CourseFolderDto updateAndGet(CourseFolderDto dto, long updateUserId) {
        //无需继承
        return null;
    }

    @Override
    public CourseFolderDto refreshUniqueName(CourseFolderDto dto, long updateUserId) {
        //无需继承
        return null;
    }

    @Override
    public List<String> findFolderNameLike(Long parentId,Integer excludeId,String pattern) {
        return courseFolderMapper.findFolderNameLike(parentId, excludeId,pattern);
    }

    @Override
    public Long findFolderNameForCreate(Long parentId, String folderName) {
        return courseFolderMapper.findFolderNameForCreate(parentId, folderName);
    }

    @Override
    public Long findFolderNameForUpdate(Long folderId, String folderName) {
        return courseFolderMapper.findFolderNameForUpdate(folderId, folderName);
    }

    @Override
    public CourseFolderRootAndDirDto findRootAndDir(Long courseId, String folderName) {
        CourseFolderRootAndDirDto ret = new CourseFolderRootAndDirDto();
        List<CourseFolder> rootAndDir = courseFolderMapper.findRootAndDir(courseId, folderName);
        for (CourseFolder courseFolder : rootAndDir) {
            if (courseFolder.getParentId() == null) {
                ret.setRootDirId(courseFolder.getId());
            }else {
                ret.setSpeDirId(courseFolder.getId());
                ret.setPrivilege(courseFolder.getPrivilege());
            }
        }
        return ret;
    }

    @Override
    public CourseFolder toModel(CourseFolderDto dto) {
        if(dto == null) return null ;
        CourseFolder model = new CourseFolder() ;
        BeanHelper.copyProperties(dto,model);
        return model ;
    }

    @Override
    public CourseFolderDto toDto(CourseFolder model) {
        if(model == null) return null ;
        CourseFolderDto dto = new CourseFolderDto() ;
        BeanHelper.copyProperties(model,dto);
        return dto ;
    }

    @Override
    public Integer create(CourseFolderDto dto, long creator) {
        dto.setUserId(creator);
        CourseFolder model = toModel(dto);

        courseFolderMapper.save(model);

        Integer dtoId = model != null ? model.getId() : null;
        dto.setId(dtoId);

        return dtoId;
    }


    @Override
    public CourseFolderDto detail(Integer dtoId) {
        CourseFolder detail = courseFolderMapper.detail(dtoId);
        return toDto(detail);
    }

    @Override
    public List<Integer> listDtoIds(long courseId) {
        return courseFolderMapper.listIdsByCourseId((int)courseId);
    }

    @Override
    public List<CourseFolderDto> list(List<Integer> listDtoIds) {
        final List<CourseFolderDto> ret = new ArrayList<CourseFolderDto>();

        if (!CollectionUtils.isEmpty(listDtoIds)) {
            List<CourseFolder> list = courseFolderMapper.list(listDtoIds);
            for (CourseFolder model : list) {
                ret.add(toDto(model));
            }
        }
        return ret;
    }

    @Override
    public void update(CourseFolderDto dto, long updateUserId) {
        CourseFolder model = toModel(dto);
        courseFolderMapper.update(model);
    }

    @Override
    public void delete(Integer dtoId, long deleteUserId) {
        courseFolderMapper.delete(dtoId);
    }

    @Override
    public void deleteMulti(List<Integer> dtoIds, long deleteUserId) {
        courseFolderMapper.deleteMulti(dtoIds);
    }

    @Override
    public void sort(List<CourseFolderDto> dbDtoList, long updateUserId) {

    }

    @Override
    public void updateMulti(List<CourseFolderDto> courseFolderDtos, long updateUserId) {
        List<Integer> idList = new ArrayList<Integer>(courseFolderDtos.size());
        Long parentId = null;
        Integer privilege = null;
        for (CourseFolderDto dto : courseFolderDtos) {
            idList.add(dto.getId());

            parentId = dto.getParentId();
            privilege = dto.getPrivilege();
        }

        courseFolderMapper.updateMulti(idList,parentId,privilege);
    }

    @Override
    public List<Integer> findDirectChildren(List<Integer> folderIds) {
        return courseFolderMapper.findDirectChildren(folderIds);
    }

    @Override
    public List<Integer> findAllChildren(List<Integer> folderIds) {
        return null;
    }
}
